package com.haiyou.data.common.persister.mysql.help.invoke;

import java.util.concurrent.atomic.AtomicBoolean;

import com.alibaba.fastjson2.util.TypeUtils;
import com.haiyou.common.entity.Entity;
import com.haiyou.data.common.persister.mysql.help.ColumnHelper;
import com.haiyou.data.common.persister.mysql.meta.ColumnMeta;


public class ByteHelper implements InvokeHelper {

	@Override
	public void invoke(ColumnMeta columnMeta, Object ov, Entity<?> entity) {
		Byte v = TypeUtils.cast(ov, Byte.class);
		try {

			if (ColumnHelper.getGenericType(columnMeta) == AtomicBoolean.class) {

				ColumnHelper.getSetter(columnMeta).invoke(entity, new AtomicBoolean(v.byteValue() == 1 ? true : false));

			} else if (ColumnHelper.getGenericType(columnMeta) == Boolean.class) {

				ColumnHelper.getSetter(columnMeta).invoke(entity, Boolean.valueOf(v.byteValue() == 1 ? true : false));

			} else if (ColumnHelper.getGenericType(columnMeta) == boolean.class) {

				ColumnHelper.getSetter(columnMeta).invoke(entity, v.byteValue() == 1 ? true : false);

			} else if (ColumnHelper.getGenericType(columnMeta) == Byte.class) {

				ColumnHelper.getSetter(columnMeta).invoke(entity, v);

			} else if (ColumnHelper.getGenericType(columnMeta) == byte.class) {

				ColumnHelper.getSetter(columnMeta).invoke(entity, v.byteValue());

			} else {

				throw new RuntimeException("unknown:" + columnMeta);

			}
		} catch (Exception e) {
			throw new RuntimeException(e.getMessage() + ":" + ov + ",\n" + entity + ",\n" + columnMeta, e);
		}
	}

}
